package com.zhangxin.aiInterview.manager.disruptor;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import com.lmax.disruptor.dsl.Disruptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * disruptor配置类
 * 配置对应的事件和消费者
 */
@Configuration
@Slf4j
public class WsEventDisruptorConfig {
    @Resource
    private WsEventWorkHandler wsEventWorkHandler;

    /**
     * 定义队列
     * @return
     */
    @Bean("chatWordsEventDisruptor")
    public Disruptor<WsEvent> messageRingBuffer(){
        //队列长度
        int bufferSize =1024 * 256;
        //设置队列
        Disruptor<WsEvent> disruptor = new Disruptor<>(
                WsEvent::new,
                bufferSize,
                ThreadFactoryBuilder.create().setNamePrefix("chatWordsEventDisruptor").build()
        );
        //设置消费者
        //从工作池中获取线程处理任务
        disruptor.handleEventsWithWorkerPool(wsEventWorkHandler);
        //开启disruptor
        disruptor.start();
        return disruptor;
    }
}